<template>
  <div class="login__container">
    <div class="login__card__container">
      <div class="card__title__container">登录易直播</div>
      <el-form
        ref="ruleFormRef"
        style="max-width: 600px"
        :model="ruleForm"
        :rules="rules"
        label-width="auto"
        class="demo-ruleForm"
        status-icon
      >
        <el-form-item label="账号" prop="name">
          <template #label>
            <div>
              <i-ep-Avatar class="icon__style" />
            </div>
          </template>
          <el-input
            v-model="ruleForm.name"
            placeholder="请输入账号"
            clearable
          />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <template #label>
            <div>
              <i-ep-Lock class="icon__style" />
            </div>
          </template>
          <el-input
            v-model="ruleForm.password"
            placeholder="请输入密码"
            :type="showPassword ? 'text' : 'password'"
            clearable
          >
            <template #suffix>
              <i-ep-View
                @click="toggleShowPassword"
                class="password__icon"
                v-if="!showPassword"
              />
              <i-ep-Hide
                @click="toggleShowPassword"
                class="password__icon"
                v-else
              />
            </template>
          </el-input>
        </el-form-item>

        <el-form-item>
          <div class="form__bottom__container">
            <el-button type="primary" @click="submitForm(ruleFormRef)">
              登 录
            </el-button>
            <el-button type="default" @click="resetForm(ruleFormRef)">
              重 置
            </el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
  // import { reactive, ref } from "vue";
  import type { ComponentSize, FormInstance, FormRules } from "element-plus";
  // import { Calendar, Search } from "@element-plus/icons-vue";
  import { useRouter } from 'vue-router'

  const router = useRouter()

  interface RuleForm {
    name: string;
    password: string;
  }

  const ruleFormRef = ref<FormInstance>();
  const ruleForm = reactive<RuleForm>({
    name: "",
    password: "",
  });

  const rules = reactive<FormRules<RuleForm>>({
    name: [
      {
        required: true,
        message: "请输入账号",
        trigger: "blur",
      },
    ],
    password: [
      {
        required: true,
        message: "请输入密码",
        trigger: "blur",
      },
    ],
  });

  const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return;
    await formEl.validate((valid, fields) => {
      if (valid) {
        console.log("submit!");
        router.push({
          path: '/'
        })
      } else {
        console.log("error submit!", fields);
      }
    });
  };

  const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return;
    formEl.resetFields();
  };

  // 密码相关

  const showPassword = ref(false);
  const toggleShowPassword = () => {
    showPassword.value = !showPassword.value;
  };
</script>

<style lang="scss" scoped>
  .login__container {
    width: 100vw;
    height: 100vh;
    background-image: url("@/assets/images/login/dyhbkimg.png");
    background-repeat: no-repeat;
    background-position: 18%;
    background-size: 40%;
    position: relative;
    .login__card__container {
      position: absolute;
      right: 18%;
      top: 30%;
      width: 450px;
      min-height: 400px;
      display: flex;
      flex-direction: column;
      align-items: center;
      background-color: var(--el-bg-color-page);
      box-shadow: var(--el-box-shadow);
      border: 1px solid var(--el-border-color);
      border-radius: 8px;
      padding: 20px;
      .card__title__container {
        color: var(--el-color-primary);
        font-size: 24px;
        font-weight: bold;
        margin-bottom: 40px;
      }
      .demo-ruleForm {
        width: 99%;
      }
      .form__bottom__container {
        margin-top: 20px;
        display: flex;
        justify-content: center;
        flex: 1 1 auto;
      }
      .password__icon {
        &:hover {
          cursor: pointer;
        }
      }
    }
  }
</style>
